<template>
  <div id="dialogAddEdit">
    <el-dialog
      class="about-set"
      :title="showTitle"
      :visible.sync="dialogData.isShow"
      width="420px"
      :before-close="labelHandleClose"
    >
      <div class="form-box">
        <el-form
          ref="ruleForm"
          :model="ruleForm"
          :rules="rules"
          label-width="80px"
          class="demo-ruleForm"
        >
          <el-form-item label="字典名称" prop="name">
            <el-input v-model.trim="ruleForm.name"></el-input>
          </el-form-item>
          <el-form-item label="字典类型" prop="type">
            <el-input v-model.trim="ruleForm.type"></el-input>
          </el-form-item>
          <el-form-item label="状态" prop="status">
            <el-radio-group v-model="ruleForm.status">
              <el-radio
                v-for="item in statusList"
                :key="item.dictValue"
                :label="item.dictValue"
              >
                {{ item.dictLabel }}
              </el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="备注" prop="remark">
            <el-input v-model.trim="ruleForm.remark" type="textarea"></el-input>
          </el-form-item>
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button type="success" plain @click="cancelLabel">取 消</el-button>
        <el-button
          type="success"
          :loading="loading"
          @click="confirmLabel('ruleForm')"
          >确认</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {
  repeatDictionType,
  getDictionaryDetail,
  editDictionary,
  addDictionary
} from '@/api/system/dictionaries' // 接口
import { sysNormalDisable } from '@utils/dictionariesFunc'
export default {
  name: 'DialogAddEdit',
  msg: '字典管理-新增/编辑弹窗',
  props: ['dialogData'],
  data() {
    //校验字典类型是否重复
    var valitType = (rule, value, callback) => {
      let obj = {
        dictId: this.dictId || ''
      }
      repeatDictionType(
        this.ruleForm.type,
        obj,
        (response) => {
          if (response) {
            //true-无重复
            callback()
          } else {
            //false-有重复
            callback(new Error('字典类型已使用'))
          }
        },
        (res) => {
          callback(new Error('校验失败'))
        }
      )
    }
    return {
      isAdd: true, // 新增-true,编辑-false
      showTitle: '',
      loading: false,
      statusList: [], // 字典状态
      dictId: null, // 字典id
      ruleForm: {
        name: '', // 字典名称
        type: '', // 字典类型code
        status: '0', // 状态。0:正常;1:停用
        remark: '' //备注
      },
      rules: {
        name: [
          { required: true, message: '字典名称不能为空', trigger: 'blur' }
          /* {min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur'} */
        ],
        type: [
          { required: true, message: '字典类型不能为空', trigger: 'blur' },
          { min: 5, message: '长度最少 5 个字符', trigger: 'blur' },
          { validator: valitType, trigger: 'blur' }
        ],
        remark: {}
      }
    }
  },
  mounted() {
    this.getStatusList()
    this.isAdd = this.dialogData.type === 'add'
    this.showTitle = this.isAdd ? '新增' : '编辑'
    if (!this.isAdd) {
      // 编辑
      this.dictId = this.dialogData.dictId
      this.getFormData()
    }
  },
  methods: {
    /* 编辑时获取表单详情 */
    getFormData() {
      getDictionaryDetail(this.dictId, (response) => {
        let obj = response
        for (let key in this.ruleForm) {
          this.ruleForm[key] = obj[key]
        }
      })
    },
    labelHandleClose(done) {
      this.cancelLabel()
      done()
    },
    cancelLabel() {
      // 关闭前清空
      this.loading = false
      this.dictId = null
      this.dialogData.dictId = null
      this.dialogData.isShow = false
    },
    /* 确认提交 */
    confirmLabel(formName) {
      /* let _this = this*/
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.subForm()
          /* Promise.all([_this.checkdictType()]).then((datas) => {
            _this.subForm()
          })*/
        } else {
          return false
        }
      })
    },
    /* 提交表单 */
    subForm() {
      this.loading = true
      let obj = { ...this.ruleForm }
      if (this.isAdd) {
        // 新增
        addDictionary(
          obj,
          (response) => {
            this.loading = false
            this.$emit('refresh')
            this.cancelLabel()
          },
          (response) => {
            this.loading = false
            this.$message.warning(this.showTitle + '失败，' + response.message)
          }
        )
      } else {
        // 编辑
        editDictionary(
          this.dictId,
          obj,
          (response) => {
            this.loading = false
            this.$emit('refresh')
            this.cancelLabel()
          },
          (response) => {
            this.loading = false
            this.$message.warning(this.showTitle + '失败，' + response.message)
          }
        )
      }
    },
    /* 获取字典状态 */
    async getStatusList() {
      let data = await sysNormalDisable('sys_normal_disable')
      this.statusList = this.statusList.concat(data)
    }
  }
}
</script>

<style lang="less">
// #dialogAddEdit {
// }
</style>
